<?php

import('jinghuan.Des');

class BaseApiAction
{

    const res_key = 'Q!W@E#R$';
    const pwd_str_format = '%s|%s|www.jinghuan.info';

    var $user_id = 0;


    function __call($name, $arguments)
    {
        // TODO: Implement __call() method.
    }

    function __construct()
    {
        $Auth = $_SERVER['HTTP_AUTH'];
        $version = $_SERVER['HTTP_OS_VERSION'];
        $des = new Des($this::res_key);
        $json = json_decode($des->decrypt($Auth), true);
        if ($json) {
            $this->user_id = $json['user_id'];
        }
    }


    protected function check_auth()
    {
        return false;
    }

    /**
     * 回调数据
     */
    protected function callback($data = null, $extend = null, $msg = null, $status = 0)
    {
        send_http_status(200);

        $value = array(
            'status' => $status,
            'extend' => $extend,
            'msg' => $msg,
        );

        if ($data !== null) {
            $value['data'] = $data;
        }


        if (isset($_REQUEST['callback'])) {
            echo htmlspecialchars($_GET["callback"]) . '(' . json_encode($value) . ')';
        } else {
            echo json_encode($value);
        }
    }

    /**
     * @param $model /Model
     * @param $check
     * @return mixed
     */
    protected function default_post($model, $like = array())
    {
        $page = I('post.page');
        $length = I('post.length');
        $order = I('post.order');
        $where = I('post.where');
        $page = $page ? $page : 0;
        $length = $length ? $length : 10;
        $order = $order ? $order : 'id desc';
        foreach ($like as $key => $val) {
            if ($where[$val])
                $where[$val] = array('like', '%' . $where[$val] . '%');
        }

        $data = $model
            ->page($page, $length)
            ->where($where)
            ->order($order)
            ->select();
        if ($data === false)
            return [];
        return $data;
    }


    protected function default_post2($model, $like = array())
    {
        $page = I('post.page');
        $length = I('post.length');
        $order = I('post.order');
        $where = I('post.where');
        $page = $page ? $page : 0;
        $length = $length ? $length : 10;
        $order = $order ? $order : 'id desc';
        foreach ($like as $key => $val) {
            if ($where[$val])
                $where[$val] = array('like', '%' . $where[$val] . '%');
        }
        $count = $model->where($where)->count();
        $num = $count ? ceil($count / $length) : 0;
        $data = $model
            ->page($page, $length)
            ->where($where)
            ->order($order)
            ->select();
        if ($data === false)
            return [];
        $arr['data'] = $data;
        $arr['page'] = $num;
        return $arr;
    }

    /**
     * @param $model
     * @param $check
     * @param array $where
     * @return array
     * 副本
     */
    protected function default_post_copy($model, $check, $where = [])
    {
        $post = I('post.');


        if ($post['limit']) {
            $model->limit($post['limit']);
        }

        if ($post['page']) {
            $model->page($post['page']);
        }

        if ($post['order']) {
            $model->order($post['order']);
        }
        if ($post['pid']) {
            $model->where("pid=$post[pid]");
        }
        if ($post['tid']) {
            $model->where("tid=$post[tid]");
        }
        if ($post['unique']) {
            $model->where(["unique" => $post['unique']]);
        }


        $w = [];
        foreach ($post as $k => $v) {
            if (in_array($k, $check)) {
                $w[$k] = $v;
            }
        }

        $w = array_merge($w, $where);
        $model->where($w);
        $data = $model->select();
        if ($data === false)
            return [];
        return $data;
    }
}
